# -*- coding:utf-8 -*-
from flask import Flask, request, jsonify, abort
from flask_cors import CORS

from com.zzh.dao.indentDao import IndentAPI

from . import app

CORS(app, supports_credentials=True)

'''项目订单表 zzh_indent API'''


@app.route('/query-indent/<int:index>', methods=['POST'])  # 查询zzh_indent表。按照传递的user_id进行查询，一个用户可以有多张订单
def queryIndent(index):
    """
    根据页码索引和用户ID查询用户订单表列表
    :param index: 页码索引
    :param userId: 用户ID，通过json格式传参
    :return:
    """
    userId = request.json.get('userId', )  # 按照传递的user_id进行查询 ，必须传递此参数
    if not userId:
        return jsonify({"messgae": {"code": 0, "info": "用户ID未传递"}})
    else:
        queryResultOfDatabaseIndent, indentListJson = IndentAPI().queryIndent(index, userId)  # 返回值的获取顺序要匹配
        if queryResultOfDatabaseIndent:
            return jsonify({"indentListJson": indentListJson, "totalCount": queryResultOfDatabaseIndent,
                            "message": {"code": 1, "info": "查询订单表成功"}})
        else:
            return jsonify({"message": {"code": 0, "info": "未查询到订单信息"}})


@app.route('/query-indent-by-status/<int:index>',
           methods=['POST'])  # 查询zzh_indent表。按照传递的user_id进行查询，一个用户可以有多张订单,查询的是默认前30条记录
def queryIndentByStatus(index):
    userId = request.json.get('userId', )  # 按照传递的user_id进行查询 ，必须传递此参数
    indentStatus = request.json.get('indentStatus', "")  # 按照订单状态进行查询，必须传递此参数
    if not userId or not indentStatus:
        return jsonify({"messgae": {"code": 0, "info": "用户和订单状态未传递"}})
    else:
        queryResultOfDatabaseIndent, indentListJson = IndentAPI().queryIndentByStatus(index, userId,
                                                                                      indentStatus)  # 返回值的获取顺序要匹配
        if queryResultOfDatabaseIndent:
            return jsonify({"indentListJson": indentListJson, "totalCount": queryResultOfDatabaseIndent,
                            "message": {"code": 1, "info": "查询订单表成功"}})
        else:
            return jsonify({"message": {"code": 0, "info": "未查询到订单信息"}})


@app.route('/add-indent/', methods=['POST'])
def addIndent():
    if not request.json:
        abort(400)
    addIndent = {
        'userId': request.json.get('userId', ),  # 必填，请传递itemId
        'indentSum': request.json.get('indentSum', ),  # 必填，请传递indentSum
        'indentMessage': request.json.get('indentMessage', ""),
        'indentCode': request.json.get('indentCode', ""),  # 必填，请传递item_id
        'indentStatus': request.json.get('indentStatus', ),  # 必填，请传递user_id
        'invoiceId': request.json.get('invoiceId', 0),
        'itemId': request.json.get('itemId', ),  # 必填，请传递item_id
        'processCode': request.json.get('processCode', "")  # 必填，请传递user_id

    }
    if not addIndent:
        return jsonify({"message": {"code": 0, "info": "订单信息填写未完整"}})
    addIndentResult = IndentAPI().addIndent(addIndent)
    if addIndentResult:
        return jsonify({"addDeclarInfo": addIndent,
                        "massage": {"code": 1, "info": "addIndent OK"}})  # 添加函数调用成功，返回添加的信息（不包括message_id）
    else:
        return jsonify({"message": {"code": 0, "info": "addIndent Failed"}})  # 添加函数调用失败


@app.route('/update-indent/<int:indentId>', methods=['POST'])
def updateIndent(indentId):
    if not request.json:
        abort(400)
    oldindent = IndentAPI().query_indent(indentId)
    updateIndent = {
        'userId': request.json.get('userId', oldindent[1]),  # 必填，请传递itemId
        'indentSum': request.json.get('indentSum', oldindent[2]),  # 必填，请传递indentSum
        'indentMessage': request.json.get('indentMessage', oldindent[3]),
        'indentCode': request.json.get('indentCode', oldindent[4]),  # 必填，请传递item_id
        'indentStatus': request.json.get('indentStatus', oldindent[5]),  # 必填，请传递user_id
        'invoiceId': request.json.get('invoiceId', oldindent[6]),
        'itemId': request.json.get('itemId', oldindent[7]),  # 必填，请传递item_id
        'processCode': request.json.get('processCode', oldindent[8])  # 必填，请传递user_id
    }
    if not updateIndent:
        return jsonify({"message": {"code": 0, "info": "信息填写未完整"}})
    else:
        updateIndentResult = IndentAPI().updateIndentById(updateIndent, indentId)
        if updateIndentResult:
            return jsonify({"updateIndentInfo": updateIndent,
                            "massage": {"code": 1, "info": "updateIndent OK"}})  # 添加函数调用成功，返回添加的信息（不包括message_id）
        else:
            return jsonify({"message": {"code": 0, "info": "updateIndent Failed"}})  # 添加函数调用失败


@app.route('/delete-indent/<int:indentId>', methods=['DELETE'])  # 根据invoiceId删除invoice表指定记录
def deleteIndent(indentId):
    deleteIndentResult = IndentAPI().deleteIndentById(indentId)
    if deleteIndentResult:
        return jsonify({"message": {"code": 1, "info": "删除订单成功！"}})
    else:
        return jsonify({"message": {"code": 0, "info": "删除订单失败！"}})


# 根据指定订单Id查询item详情
@app.route('/query-indent-by-indentId', methods=['POST'])  # 根据指定的订单ID查询响应的Item详情
def queryIndentByIndentId():
    indentId = request.json.get('indentId', )  # 按照传递的indentId进行查询 ，必须传递此参数
    queryResultOfDatabaseIndent, indentListJson = IndentAPI().queryIndentByIndentId(indentId)  # 返回值的获取顺序要匹配
    if queryResultOfDatabaseIndent:
        return jsonify({"indentListJson": indentListJson, "totalCount": queryResultOfDatabaseIndent,
                        "message": {"code": 1, "info": "查询订单表成功"}})
    else:
        return jsonify({"message": {"code": 0, "info": "未查询到订单信息"}})
